<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="名称" prop="categoryName" label-width="68px">
        <el-input v-model="queryParams.categoryName" placeholder="名称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商城" prop="parentId">
        <el-select v-model="queryParams.parentId" clearable style="width: 240px">
          <el-option v-for="dict in this.parentShopName" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动标记" prop="marketFlag">
        <el-select v-model="queryParams.marketFlag" clearable style="width: 240px">
          <el-option v-for="dict in this.marketFlag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="showFlag">
        <el-select v-model="queryParams.showFlag" clearable style="width: 240px">
          <el-option v-for="dict in this.showFlag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['showroom:consumeCategory:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" ref="elTable" @sort-change="changeTableSort" v-loading="loading" :data="list" :row-key="getRowKeys" @row-dblclick="handleUpdate" stripe border>
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="分类名字" prop="categoryName" width="120" align="center" :sortable="'custom'" />
      <el-table-column label="父分类" prop="parentId" width="150" align="center" :sortable="'custom'" >
        <template slot-scope="scope">
          <i v-if="scope.row.parentId == '1'">云派商城</i>
          <i v-if="scope.row.parentId == '2'">品牌商城</i>
        </template>
      </el-table-column>
      <el-table-column label="活动" prop="marketFlag" width="120" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          {{selectDictLabel(marketFlag,scope.row.marketFlag)}}
        </template>
      </el-table-column>
      <el-table-column label="是否显示" prop="showFlag" width="120" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          {{selectDictLabel(showFlag,scope.row.showFlag)}}
        </template>
      </el-table-column>
      <el-table-column label="缩略图" prop="imgMin" align="center" width="120">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgMin"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="第三方地址" prop="imgLink" min-width="200" align="center" />

      <el-table-column label="排序" prop="seq" min-width="120" align="center" :sortable="'custom'" />

      <el-table-column label="操作" fixed="right" align="center" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['showroom:consumeCategory:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)" v-hasPermi="['showroom:consumeCategory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm('form')">
      <form-view :form="form" ref="form" :showFlag="showFlag" :marketFlag="marketFlag" :parentShopName="parentShopName"></form-view>
    </Z-Dialog>
  </div>
</template>

<script>
import { listConsumeCategory, addConsumeCategory, editConsumeCategory, delConsumeCategory } from "@/api/showroom/goods";
import formView from './formView.vue'
import { deepClone } from '@/utils/index'
import tableImage from "@/components/TableImageView/index.vue";

export default {
  name: "ConsumeCategory", //消费品目录
  components: { formView, tableImage },
  data() {
    return {
      loading: true, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      list: [], //商品列表
      title: "",  // 弹出层标题
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,
      form: {},   // 表单参数
      marketFlag: [
        { label: '普通商品目录', value: '0' },
        { label: '金积分活动目录', value: '1' },
        { label: '第三方地址', value: '2' },
        { label: '星级奖品目录', value: '3' },
      ],
      showFlag: [
        { label: '不显示', value: '0' },
        { label: '显示', value: '1' },
      ],
      parentShopName: [
        { label: '云派商城', value: 1 },
        { label: '品牌商城', value: 2 }
      ]
    };
  },

  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {
        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 330px)'
    },
  },

  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      listConsumeCategory(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.list = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },

    getRowKeys(row) {
      return row.id
    },

    //列表选中
    handleSelectionChange(val) {
      this.batchUpParams.ids = val.map((item) => {
        return item.id;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      // this.resetForm("form");
      this.form = {};
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消费品分类";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      // if (!hasPer.isPerShow([''])) {
      //   return;
      // }
      this.reset();
      this.open = true;
      this.title = "编辑消费品分类";
      this.form = deepClone(row);
      this.form.marketFlag = String(row.marketFlag);
      this.form.showFlag = String(row.showFlag);

    },

    /** 提交按钮 */
    submitForm(refStr) {
      let ref = this.$refs[refStr];
      let params = ref.form;
      if (params.marketFlag != '2') {
        params.imgLink = ''
      }

      ref.$refs["form"].validate((valid) => {
        if (valid) {
          if (params.id != undefined) {
            editConsumeCategory(params).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumeCategory(params).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除此数据项？')
        .then(() => {
          delConsumeCategory({ categoryId: row.id }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        }
        ).catch(() => { });
    },


  },
};
</script>
<style scoped>
.input-W {
  width: 550px;
}
</style>
